package com.demo.designmode.proxy.case1;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author jiaxXM
 * @date 2024/9/3
 */
public class LogRecordProxy <T> implements InvocationHandler {

    private T target;

    public LogRecordProxy(T t){
        this.target = t;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("记录日志：执行" + method.getName() + "方法前");
        Object result = method.invoke(target, args);
        System.out.println(result);
        System.out.println("记录日志：执行" + method.getName() + "方法后");
        return result;
    }

    /**
     * 获取代理对象的方法
     */
    @SuppressWarnings("unchecked")
    public <T> T getProxy() throws Exception {
        return (T) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }
}
